// 解析createRouter函数,createWebHashHistory函数
import { createRouter, createWebHashHistory } from "vue-router";
import { useIndexStore } from "../pinia";
import { request } from '@/utils/axios/request.js'
// 引入首页
import Index from "@/pages/Index/Index.vue";
import mitter from "../utils/mitter"

// 配置路由表 routes
const routes = [
  {
    path: "/",
    redirect: "/index",
  },
  {
    path: "/index",
    name: "index",
    component: Index,
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../pages/Login/Login.vue"),
  },
  {
    path: "/menu",
    name: "menu",
    component: () => import("../pages/Menu/Menu.vue"),
  },
  {
    path: "/message",
    name: "message",
    component: () => import("../pages/Message/Message.vue"),
  },
  {
    path: "/systemlog",
    name: "systemlog",
    component: () => import("../pages/SystemLog/SystemLog.vue"),
  },
  {
    path: "/department",
    name: "department",
    component: () => import("../pages/Department/Department.vue"),
  },
  {
    path: '/workingday',
    name: 'workingday',
    component: () => import('../pages/Workingday/Workingday.vue')
  },
  {
    path: "/center",
    name: "center",
    component: () => import("../pages/Center/Center.vue"),
  },
  {
    path: "/salary",
    name: "salary",
    component: () => import("../pages/Salary/Salary.vue"),
  },
  {
    path: "/character",
    name: "character",
    component: () => import("../pages/Character/Character.vue"),
  },
  {
    path: "/business",
    name: "business",
    component: () => import("../pages/Business/Business.vue"),
  },
  {
    path: "/replacementCard",
    name: "replacementCard",
    component: () => import("../pages/ReplacementCard/ReplacementCard.vue"),
  },
  {
    path: "/clockingDetails",
    name: "clockingDetails",
    component: () => import("../pages/ClockingDetails/ClockingDetails.vue"),
  },
  {
    path: "/systemConfiguration",
    name: "systemConfiguration",
    component: () => import("../pages/SystemConfiguration/SystemConfiguration.vue"),
  },
  {
    path: "/applicationForExtraWork",
    name: "applicationForExtraWork",
    component: () => import("../pages/ApplicationForExtraWork/ApplicationForExtraWork.vue"),
  },
  {
    path: "/leaveRequest",
    name: "leaveRequest",
    component: () => import("../pages/LeaveRequest/LeaveRequest.vue"),
  },
  {
    path: "/employeeLeaveDetails",
    name: "employeeLeaveDetails",
    component: () => import("../pages/EmployeeLeaveDetails/EmployeeLeaveDetails.vue"),
  },
  {
    path: "/user",
    name: "user",
    component: () => import("../pages/User/User.vue"),
  },
  {
    path: "/chat",
    name: "chat",
    component: () => import("../pages/Chat/chat.vue"),
  },
  {
    path: "/:error*",
    name: "notfound",
    component: () => import("@/pages/404/NotFound.vue"),
  },


];

// 创建路由实例 router
const router = createRouter({
  // 路由模式：1.hash 2.history
  history: createWebHashHistory(),
  routes,
});

// 全局前置导航守卫
router.beforeEach(async (to, from, next) => {
  let store = useIndexStore()
  let username = localStorage.getItem('username')
  // 验证token是否过期
  let token = localStorage.getItem('token')
  if (token) {
    try {
      let newdata = await request({
        url: `/admin/token`,
        method: 'post',
        data: { token }
      })
    } catch (err) {
      localStorage.removeItem('username')
      localStorage.removeItem('token')
      router.push('/login')
      console.log(err);
    }
  }
  //根据权限转动态路由
  if (username) {
    store.roleChange(username)
  }
  if (to.path == '/' || to.path == '/index' || to.path == '/login') {
    mitter.emit('routerNavNum', '首页')
    next()
  } else {
    if (username) {
      next()
    } else {
      router.push('/login')
    }
  }
})



// 暴露路由实例 router
export default router;
